package org.jeecg.modules.bus.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.bus.entity.*;
import org.jeecg.modules.bus.service.IBusTenderApplicationService;
import org.jeecg.modules.bus.entity.*;
import org.jeecg.modules.bus.service.IBusProjectBasicService;
import org.jeecg.modules.bus.service.IBusRiskanalysisDataService;
import org.jeecg.modules.bus.service.IBusTenderApplicationProInfTrackService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 商务管理（投标申请）
 * @Author: jeecg-boot
 * @Date:   2022-11-15
 * @Version: V1.0
 */
@Api(tags="商务管理（投标申请）")
@RestController
@RequestMapping("/bus/busTenderApplication")
@Slf4j
public class BusTenderApplicationController extends JeecgController<BusTenderApplication, IBusTenderApplicationService> {
	@Autowired
	private IBusTenderApplicationService busTenderApplicationService;
	@Autowired
	private IBusRiskanalysisDataService busRiskanalysisDataService;
	@Autowired
	private IBusProjectBasicService busProjectBasicService;

	@Autowired
	private IBusTenderApplicationProInfTrackService busTenderApplicationProInfTrackService;

	/**
	 * 分页列表查询
	 *
	 * @param busTenderApplication
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "商务管理（投标申请）-分页列表查询")
	@ApiOperation(value="商务管理（投标申请）-分页列表查询", notes="商务管理（投标申请）-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BusTenderApplication busTenderApplication,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BusTenderApplication> queryWrapper = QueryGenerator.initQueryWrapper(busTenderApplication, req.getParameterMap());
		Page<BusTenderApplication> page = new Page<BusTenderApplication>(pageNo, pageSize);
		IPage<BusTenderApplication> pageList = busTenderApplicationService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 选择风险分析
	  *
	  * @param busRiskanalysisData
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "商务管理（投标申请）-选择风险分析")
	 @ApiOperation(value="商务管理（投标申请）-选择风险分析", notes="商务管理（投标申请）-选择风险分析")
	 @GetMapping(value = "/listRisk")
	 public Result<?> querylistRisk(BusRiskanalysisData busRiskanalysisData,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 // 1.先获取所有的最新的信息跟踪
		 QueryWrapper<BusRiskanalysisData> queryWrapper = new QueryWrapper<>();
		 QueryGenerator.installMplus(queryWrapper,busRiskanalysisData, req.getParameterMap());

		 Page<BusRiskanalysisData> page = new Page<>(pageNo, pageSize);
		 // 2.筛选过没有被选择的信息跟踪
		 //	3.如果风险分析的下的项目清单全部被选择，则需要过滤此条风险分析；
		 // 4.返回 pageList
		 IPage<BusRiskanalysisData> pageList = busRiskanalysisDataService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	 /**
	  * 根据风险分析ids查询
	  *
	  * @param riskIds
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "商务管理(投标申请)数据表-根据风险分析ids查询")
	 @ApiOperation(value="商务管理(投标申请)数据表-根据风险分析ids查询", notes="商务管理(投标申请)数据表-根据风险分析ids查询")
	 @GetMapping(value = "/listByRiskIds")
	 public Result<?> queryPageListByRiskIds(BusProjectBasic busProjectBasic,
											 @RequestParam(name="riskIds") String riskIds,
											 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
											 HttpServletRequest req) {
		 QueryWrapper<BusProjectBasic> queryWrapper = new QueryWrapper<>();
		 String[] ids=riskIds.split(",");
		 List<String> idList= Arrays.asList(ids);
		 queryWrapper.select(BusProjectBasic.class,item -> !item .getColumn().equals("id")).in("risk_analysis_id",idList);
		 QueryGenerator.installMplus(queryWrapper,busProjectBasic, req.getParameterMap());
		 Page<BusProjectBasic> page = new Page<>(pageNo, pageSize);
		 IPage<BusProjectBasic> pageList = busProjectBasicService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	/**
	 *   添加
	 *
	 * @param busTenderApplicationVO
	 * @return
	 */
	@AutoLog(value = "商务管理（投标申请）-添加")
	@ApiOperation(value="商务管理（投标申请）-添加", notes="商务管理（投标申请）-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BusTenderApplicationVO busTenderApplicationVO) {
		 busTenderApplicationService.save(busTenderApplicationVO.getBusTenderApplication());
		 String id = busTenderApplicationVO.getBusTenderApplication().getId();
		 List<BusTenderApplicationProInfTrack> busTenderApplicationProInfTrackList = busTenderApplicationVO.getBusTenderApplicationProInfTrackList();
		 for (BusTenderApplicationProInfTrack busTenderApplicationProInfTrack : busTenderApplicationProInfTrackList){
			 busTenderApplicationProInfTrack.setTenderApplicationId(id);
			 busTenderApplicationProInfTrackService.save(busTenderApplicationProInfTrack);
		 }
		 return Result.OK("添加成功！");
	 }

	/**
	 *  编辑
	 *
	 * @param busTenderApplication
	 * @return
	 */
	@AutoLog(value = "商务管理（投标申请）-编辑")
	@ApiOperation(value="商务管理（投标申请）-编辑", notes="商务管理（投标申请）-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BusTenderApplication busTenderApplication) {
		busTenderApplicationService.updateById(busTenderApplication);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商务管理（投标申请）-通过id删除")
	@ApiOperation(value="商务管理（投标申请）-通过id删除", notes="商务管理（投标申请）-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		busTenderApplicationService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "商务管理（投标申请）-批量删除")
	@ApiOperation(value="商务管理（投标申请）-批量删除", notes="商务管理（投标申请）-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.busTenderApplicationService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商务管理（投标申请）-通过id查询")
	@ApiOperation(value="商务管理（投标申请）-通过id查询", notes="商务管理（投标申请）-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BusTenderApplication busTenderApplication = busTenderApplicationService.getById(id);
		if(busTenderApplication==null) {
			return Result.error("未找到对应数据");
		}
		// 1.根据投标申请得到风险分析id
		String riskIds = busTenderApplication.getProRiskId();
		String[] ids=riskIds.split(",");
		List<String> idList= Arrays.asList(ids);
		QueryWrapper<BusRiskanalysisData> queryWrapper = new QueryWrapper<>();
		// 2.根据风险分析id得到名称
		queryWrapper.select("id","risk_name").in("id",idList);
		List<BusRiskanalysisData> busRiskanalysisDataList = busRiskanalysisDataService.list(queryWrapper);
		//	StringBuilder sb = new StringBuilder();
		//	for(BusRiskanalysisData busRiskanalysisData: busRiskanalysisDataList){
		//		sb.append(busRiskanalysisData.getRiskName()+',');
		//	}
		//	String s = sb.substring(0, sb.length()-1);
		QueryWrapper<BusTenderApplicationProInfTrack> queryWrapper1 = new QueryWrapper<>();
		queryWrapper1.eq("tender_application_id",id);
		List<BusTenderApplicationProInfTrack> busTenderApplicationProInfTrackList = busTenderApplicationProInfTrackService.list(queryWrapper1);

		BusTenderApplicationVO busTenderApplicationVO = new BusTenderApplicationVO();
		busTenderApplicationVO.setBusTenderApplication(busTenderApplication);
		busTenderApplicationVO.setBusTenderApplicationProInfTrackList(busTenderApplicationProInfTrackList);
		busTenderApplicationVO.setRisks(busRiskanalysisDataList);

		return Result.OK(busTenderApplicationVO);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param busTenderApplication
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BusTenderApplication busTenderApplication) {
        return super.exportXls(request, busTenderApplication, BusTenderApplication.class, "商务管理（投标申请）");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BusTenderApplication.class);
    }

}
